package soms_handler

import (
	"encoding/json"
	"gitlab.yoyiit.com/youyi/go-core/handler"
	"gitlab.yoyiit.com/youyi/go-core/util"
	"github.com/gin-gonic/gin"
	"github.com/pkg/errors"
	"strconv"
	"time"
)

type ProductCustomValueHandler interface {
	ListProductCustomValue() gin.HandlerFunc
	GetProductCustomValue() gin.HandlerFunc
	AddProductCustomValue() gin.HandlerFunc
	EditProductCustomValue() gin.HandlerFunc
	DeleteProductCustomValue() gin.HandlerFunc
	BatchDeleteProductCustomValue() gin.HandlerFunc
}

type productCustomValueHandler struct {
	somsClient soms.Client
}

func (h *productCustomValueHandler) ListProductCustomValue() gin.HandlerFunc {
	return func(ctx *gin.Context) {
		var req stru.ListProductCustomValueDataRequest
		if err := ctx.ShouldBindQuery(&req); err != nil {
			handler.HandleErrorResponse(ctx, err)
			return
		}
		res, err := h.somsClient.ListProductCustomValue(util.GetRPCContext(ctx), &api.ListProductCustomValueRequest{
			PageNum:  req.PageNum,
			PageSize: req.PageSize,
			Data: &api.ProductCustomValueData{
				SomsProductId: req.SomsProductId,
	FieldId: req.FieldId,
	FieldName: req.FieldName,
	FieldType: req.FieldType,
	PrintFlag: req.PrintFlag,
	Sort: req.Sort,
	Value: req.Value,
	
			},
		})
		if err != nil {
			handler.HandleErrorResponse(ctx, err)
			return
		}
		if res == nil || res.Data == nil {
			handler.HandleSuccessListDataResponse(ctx, nil, 0)
			return
		}
		handler.HandleSuccessListDataResponse(ctx, res.Data, res.Count)
	}
}

func (h *productCustomValueHandler) GetProductCustomValue() gin.HandlerFunc {
	return func(ctx *gin.Context) {
		id, err := strconv.ParseInt(ctx.Param("id"), 10, 0)
		if err != nil {
			handler.HandleErrorResponse(ctx, err)
			return
		}
		res, err := h.somsClient.GetProductCustomValue(util.GetRPCContext(ctx), &api.ProductCustomValueData{
			Id: id,
		})
		if err != nil {
			handler.HandleErrorResponse(ctx, err)
			return
		}
		handler.HandleSuccessDataResponse(ctx, res)
	}
}

func (h *productCustomValueHandler) AddProductCustomValue() gin.HandlerFunc {
	return func(ctx *gin.Context) {
		var data stru.ProductCustomValueData
		if err := ctx.ShouldBindJSON(&data); err != nil {
			handler.HandleErrorResponse(ctx, err)
			return
		}
		addData := &api.ProductCustomValueData{
            Id:         data.Id,
            SomsProductId: data.SomsProductId,
	FieldId: data.FieldId,
	FieldName: data.FieldName,
	FieldType: data.FieldType,
	PrintFlag: data.PrintFlag,
	Sort: data.Sort,
	Value: data.Value,
	
        }
		err := h.somsClient.AddProductCustomValue(util.GetRPCContext(ctx), addData)
		if err != nil {
			handler.HandleErrorResponse(ctx, err)
			return
		}
		handler.HandleSuccessResponse(ctx)
	}
}

func (h *productCustomValueHandler) EditProductCustomValue() gin.HandlerFunc {
	return func(ctx *gin.Context) {
		id, err := strconv.ParseInt(ctx.Param("id"), 10, 0)
		if err != nil {
			handler.HandleErrorResponse(ctx, err)
			return
		}
		var data stru.ProductCustomValueData
		if err = ctx.ShouldBindJSON(&data); err != nil {
			handler.HandleErrorResponse(ctx, err)
			return
		}

		editData := &api.ProductCustomValueData{
            Id:         id,
            SomsProductId: data.SomsProductId,
	FieldId: data.FieldId,
	FieldName: data.FieldName,
	FieldType: data.FieldType,
	PrintFlag: data.PrintFlag,
	Sort: data.Sort,
	Value: data.Value,
	
        }
		if err = h.somsClient.EditProductCustomValue(util.GetRPCContext(ctx), editData); err != nil {
			handler.HandleErrorResponse(ctx, err)
			return
		}
		handler.HandleSuccessResponse(ctx)
	}
}

func (h *productCustomValueHandler) DeleteProductCustomValue() gin.HandlerFunc {
	return func(ctx *gin.Context) {
		id, err := strconv.ParseInt(ctx.Param("id"), 10, 0)
		if err != nil {
			handler.HandleErrorResponse(ctx, err)
			return
		}
		if err = h.somsClient.DeleteProductCustomValue(util.GetRPCContext(ctx), id); err != nil {
			handler.HandleErrorResponse(ctx, err)
			return
		}
		handler.HandleSuccessResponse(ctx)
	}
}

func (h *productCustomValueHandler) BatchDeleteProductCustomValue() gin.HandlerFunc {
	return func(ctx *gin.Context) {
		var req stru.BatchDeleteProductCustomValueRequest
		if err := ctx.ShouldBindJSON(&req); err != nil {
			handler.HandleErrorResponse(ctx, err)
			return
		}

		if req.Ids == nil || len(req.Ids) == 0 {
			handler.HandleErrorResponse(ctx, handler.HandleNewError("参数错误"))
			return
		}

		if err := h.somsClient.BatchDeleteProductCustomValue(util.GetRPCContext(ctx), &api.BatchDeleteProductCustomValueRequest{
			Ids: req.Ids,
		}); err != nil {
			handler.HandleErrorResponse(ctx, err)
			return
		}
		handler.HandleSuccessResponse(ctx)
	}
}

